package com.tencent.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: @date: 2022/9/4 15:26
 * @description:
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {

    @Bean
    public Docket userDocket(){
        Docket docket = new Docket(DocumentationType.OAS_30);
        //设置这个接口文档的基本信息
        ApiInfo apiInfo = new ApiInfo(
                "健康系统预约历史的接口文档",
                "预约历史所有接口",
                "1.0",
                "https://tieba.baidu.com/f?kw=ikun&fr=ala0&loc=rec&dyTabStr=MCw2LDEsNCw1LDMsNyw4LDIsOQ%3D%3D",
                //制作这个接口文档的人的基本信息
                new Contact(
                        "ikun一号",
                        "https://tieba.baidu.com/f?kw=ikun&fr=ala0&loc=rec&dyTabStr=MCw2LDEsNCw1LDMsNyw4LDIsOQ%3D%3D",
                        "3128274699@qq.com"),

                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>());

        docket.apiInfo(apiInfo);

        docket
                //给这个模块定义一个组名
                .groupName("预约历史模块")
                //指定选择的接口信息
                .select()
                //指定选择的接口所在的包名
                .apis(RequestHandlerSelectors.basePackage("com.tencent.controller"))
                //执行选择的包下的哪些路径请求
                .paths(PathSelectors.ant("/orderhistory/**"))
                .build();

        //配置全局参数(当前接口文档中所有接口都需要的共同参数，比如登录后的认证信息-jwt)
        docket.globalRequestParameters(getGlobalParams());

        return docket;
    }

    private List<RequestParameter> getGlobalParams() {
        List<RequestParameter> list = new ArrayList<>();
        RequestParameter parameter = new RequestParameterBuilder()
                //全局参数名字
                .name("Authorization")
                //全局参数的描述
                .description("登录后，获取到的jwt认证信息，后续访问接口时，都必须带上这个认证信息进行登录验证")
                //要求必须携带这个参数
                .required(true)
                //参数通过请求头携带
                .in(ParameterType.HEADER)
                .build();

        list.add(parameter);
        return list;
    }
}